const CommomUtil = require('../utils/CommomUtil');
const SessionUtil = require('../utils/SessionUtil');

const user_dao = require('../dao/user_dao');
const music_dao = require('../dao/music_dao');
const redis_session = require('../config').redis_user;

/** 发布音乐 */
module.exports.pub_music = publish_music_info;
async function publish_music_info(ctx) {

    let { ty_id, ty_s_id, albums_id, singer, lyricist, composer, music_cover_id, music_audio_id, music_title, music_lrc, music_tags, music_type } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(singer, 50) || CommomUtil.inValid(lyricist, 50) || CommomUtil.inValid(composer, 50) || CommomUtil.isEmpty(music_cover_id) || CommomUtil.isEmpty(music_audio_id)
        || CommomUtil.isEmpty(albums_id) || CommomUtil.inValid(music_title, 50) || CommomUtil.inValid(music_lrc, 5000) || CommomUtil.inValid(music_tags, 100) || CommomUtil.inValid(music_type, 10)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(music_cover_id) || !CommomUtil.isNumber(music_audio_id) || !CommomUtil.isNumber(music_type)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, msg] = await music_dao.publish_music_info(ty_id, albums_id, singer, lyricist, composer, music_audio_id, music_cover_id, music_title, music_lrc, music_tags, music_type, 0);
    if (flag) {
        resData['music_id'] = msg;
        resData['ret'] = true;
        resData['msg'] = '发布成功!';
        return ctx.body = resData;
    }
    resData['ret'] = false;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 音乐详情 */
module.exports.music_detail = music_detail;
async function music_detail(ctx) {
    let { ty_id, ty_s_id, music_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(music_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let music_info = await music_dao.get_music_detail(ty_id, music_id);
    if (music_info != null) {
        let [user_name, user_img] = await user_dao.get_user_name_and_img(music_info['owner_id']);
        if (user_name != null && user_img != null) {
            resData = music_info;
            resData['user_name'] = user_name;
            resData['user_img'] = user_img;
            resData['ret'] = true;
            resData['msg'] = '查询成功!';
            return ctx.body = resData;
        }

    }
    resData['ret'] = false;
    resData['msg'] = '音乐不存在!';
    return ctx.body = resData;
}

/** 音乐 点赞/取消点赞  */
module.exports.hit_like_music = hit_like_music;
async function hit_like_music(ctx) {
    let { ty_id, ty_s_id, music_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(music_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let like_num = await music_dao.get_music_like_num(music_id);
    if (like_num == null) {
        resData['ret'] = false;
        resData['msg'] = '操作失败!';
        return ctx.body = resData;
    }
    let key = ty_id + '_like_music_' + music_id;
    let str = await redis_session.get(key);
    let like_status = true;
    if (CommomUtil.isEmpty(str)) {
        if (await redis_session.set(key, 1) == 'OK') {
            let flag = await music_dao.update_music_like_num(music_id, 1);
            if (flag) like_num += 1;
            like_status = true;
        }
    } else {
        if (await redis_session.del(key)) {
            let flag = await music_dao.update_music_like_num(music_id, -1);
            if (flag) like_num -= 1;
            like_status = false;
        }
    }
    resData['like_num'] = like_num;
    resData['like_status'] = like_status;
    resData['ret'] = true;
    resData['msg'] = '操作成功!';
    return ctx.body = resData;
}

/**因为服务器分线程压缩MP3 压缩后才会取时长，但是用户可能把音乐已经发布出去了。因此需要调用此接口
 * 更新音乐时长  */
const pwd = 'crims_1542185634';
module.exports.update_music_time = update_music_time_by_audio_id;
async function update_music_time_by_audio_id(ctx) {
    let { music_audio_id, music_time, key } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(music_audio_id) || CommomUtil.isEmpty(music_time) || CommomUtil.isEmpty(key) || pwd != key) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(music_audio_id) || !CommomUtil.isNumber(music_time)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    let [flag, msg] = await music_dao.update_music_time_by_audio_id(music_audio_id, music_time);
    resData['ret'] = flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 查询最新单曲 */
module.exports.query_single_music_list = query_single_music_list;
async function query_single_music_list(ctx) {
    let { ty_id, ty_s_id, begin, plen, type } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen) || CommomUtil.isEmpty(type)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1 || !CommomUtil.isNumber(type) || type < 1 || type > 5) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let music_list = await music_dao.query_single_music_list(begin, plen, type);
    resData['music_list'] = music_list;
    resData['ret'] = true;
    resData['msg'] = '查询成功!';
    return ctx.body = resData;
}



/** 搜索歌曲列表 */
module.exports.search_music_list = search_music_list;
async function search_music_list(ctx) {
    let { ty_id, ty_s_id, begin, plen, music_title } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen) || CommomUtil.inValid(music_title, 50)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let music_list = await music_dao.search_music_list_by_music_title(begin, plen, music_title);
    resData['music_list'] = music_list;
    resData['ret'] = true;
    resData['msg'] = '查询成功!';
    return ctx.body = resData;
}